package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.system.dao.RoomMapper;
import com.qinglei.recoup.system.domain.Room;
import com.qinglei.recoup.system.pojo.RoomPageParam;
import com.qinglei.recoup.system.pojo.RoomShowVO;
import com.qinglei.recoup.system.service.MonitorTimeService;
import com.qinglei.recoup.system.service.RoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author syp
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Override
    public CommonRes<IPage<RoomShowVO>> showPageroom(RoomPageParam roomPageParam) {
        Page<RoomShowVO> page = new Page<>(roomPageParam.getPageNum(), roomPageParam.getPageSize());
        IPage<RoomShowVO> buildingShowVOIPage = roomMapper.showPageRoom(page, roomPageParam.getFloorId());
        return CommonRes.ok(buildingShowVOIPage);
    }

    @Override
    public CommonRes addBedCnt(Integer id) {
        Room room = getBaseMapper().selectById(id);
        room.setBedCnt(room.getBedCnt() + 1);
        int count = getBaseMapper().updateById(room);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("添加层床位数量失败");
    }

    @Override
    public CommonRes subBedCnt(Integer id) {
        Room room = getBaseMapper().selectById(id);
        if (room.getBedCnt() > 0) {
            room.setBedCnt(room.getBedCnt() - 1);
        }
        int count = getBaseMapper().updateById(room);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("减少层床位数量失败");
    }

    /**
     * 修改监测时间是否统一
     *
     * @param id
     * @return
     */
    public Boolean updateMonitorTimeUnified(Integer id) {
        try {
            Room room = new Room();
            room.setId(id);
            room.setMonitorTimeUnified(MonitorTimeService.UNIFIED_YES);
            baseMapper.updateById(room);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return true;
    }

    /**
     * 根据层id查询房间
     *
     * @param floorId
     * @return
     */
    @Override
    public CommonRes<List<Room>> getRoomByFloorId(Integer floorId) {
        return CommonRes.ok(baseMapper.selectList(Wrappers.<Room>lambdaUpdate().eq(Room::getFloorId, floorId)));
    }

    @Override
    public List<Room> getRoomByBuildingId(Integer buildingId) {
        return baseMapper.selectList(Wrappers.<Room>lambdaUpdate().eq(Room::getBuildingId, buildingId));
    }

    /**
     * 修改离床阀值时间统一
     *
     * @param objId
     * @param outBedTimeout
     * @return
     */
    @Override
    public boolean updateOutBedTime(Integer objId, String outBedTimeout) {
        Room room = new Room();
        room.setId(objId);
        room.setOutBedTimeout(outBedTimeout);
        room.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_YES);
        int count = baseMapper.updateById(room);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改离床阀值时间不统一
     *
     * @param roomId
     * @return
     */
    @Override
    public boolean updateOutBedTimeNo(Integer roomId) {
        Room room = new Room();
        room.setId(roomId);
        room.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(room);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改监控时间不统一
     *
     * @param roomId
     * @return
     */
    @Override
    public boolean updateMonitorTimeNo(Integer roomId) {
        Room room = new Room();
        room.setId(roomId);
        room.setMonitorTimeUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(room);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public String getNameWithParent(Integer roomId) {
        String name = baseMapper.selectNameWithParent(roomId);
        if (name == null) {
            name = "";
        }
        return name;
    }
}
